<?php

declare(strict_types=1);

namespace App\Service;

use App\Model\SysUser as SysUserModel;

class DcenterUser extends BaseService
{
    public function login($params)
    {
        $user = SysUserModel::query()->where('username', $params['username'])->first();

        if (!$user) {
            $this->returnError('账号或密码错误', 404);
        }

        // 验证密码是否正确
        if (!password_verify($params['password'], $user['password'])) {
            $this->returnError('账号或密码错误', 404);
        }

        // 验证账户是否禁用
        if ($user['status'] != 1) {
            $this->returnError('该账户被禁用无法使用', 404);
        }

        return $user;
    }

    // 获取商户员工基本信息
    public function getInfo($uid)
    {
        return SysUserModel::query()->where('uid', $uid)->first();
    }

    // 更新商户员工信息
    public function updateUserInfoById($uid, $data)
    {
        $user = SysUserModel::query()->where('uid', $uid)->update($data);

        if (!$user) {
            $this->returnError('注册失败请稍后重试');
        }

        return $user;
    }

    // 管理员列表
    public function userList($map)
    {
        return SysUserModel::query()->where($map)->select('uid', 'username', 'real_name','last_ip','last_time','login_count','status','role_id','is_admin','created_at','updated_at')->orderBy('uid', 'asc')->get();
    }

    // 添加管理员
    public function addDcenterUser($data)
    {
        return SysUserModel::insert($data);
    }

    // 编辑管理员
    public function editDcenterUser($uid, $data)
    {
        return SysUserModel::query()->where('uid', $uid)->update($data);
    }

    // 删除管理员
    public function deleteUser($uid)
    {
        return SysUserModel::where('uid', $uid)->delete();
    }
}
